package com.xintonghua.bussiness.ui.user;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.utils.MyTimeUtils;
import com.xintonghua.base.widget.MyItemTextView;
import com.xintonghua.base.widget.ShowAllGridView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ChoosePhotoListViewAdapter;
import com.xintonghua.bussiness.adapter.ChoosePhotoListViewAdapters;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.StoreBean;
import com.xintonghua.bussiness.util.ImageTools;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.addapp.pickers.picker.TimePicker;
import me.iwf.photopicker.PhotoPicker;

/**
 * 作者：wanxianyi
 * 时间：2018/3/2.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class ShopManageActivity extends BaseActivity implements ChoosePhotoListViewAdapter.AdapterClickListener, ChoosePhotoListViewAdapters.AdapterClickListener {


    @BindView(R.id.tv_shop_name)
    EditText tvShopName;
    @BindView(R.id.tv_shop_address)
    EditText tvShopAddress;
    @BindView(R.id.tv_shop_phone)
    EditText tvShopPhone;
    @BindView(R.id.tv_shop_time)
    MyItemTextView tvShopTime;
    @BindView(R.id.gv_shop_img)
    ShowAllGridView gvShopImg;
    @BindView(R.id.gv_shop_home)
    ShowAllGridView gvShopHome;
    private String startTime = "";
    private String endTime = "";
    private String imgs, shopImgs;
    ChoosePhotoListViewAdapter choosePhotoListViewAdapter;
    ChoosePhotoListViewAdapters choosePhotoListViewAdapters;
    List<String> photosList = new ArrayList<>();
    List<String> photosLists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_shop);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        showDate = Calendar.getInstance();
        setSimpleActionBar("店铺信息", "保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(getStr(tvShopName))) {
                    mToast("请输入店铺名称");
                    return;
                }
                if (TextUtils.isEmpty(getStr(tvShopAddress))) {
                    mToast("请输入店铺地址");
                    return;
                }
                if (TextUtils.isEmpty(getStr(tvShopPhone))) {
                    mToast("请输入联系电话");
                    return;
                }
                if (TextUtils.isEmpty(startTime)) {
                    mToast("请选择开始时间");
                    return;
                }
                if (TextUtils.isEmpty(endTime)) {
                    mToast("请选择结束时间");
                    return;
                }
                List<String> imgList = MyUtils.getListStringSplitValue(imgs);
                if (imgList.size() == 0) {
                    mToast("请添加图片");
                    return;
                }
                List<String> shopList = MyUtils.getListStringSplitValue(shopImgs);
                if (shopList.size() == 0) {
                    mToast("请添加图片");
                    return;
                }
                httpCent.saveShopInfo(getStr(tvShopName), getStr(tvShopAddress), getStr(tvShopPhone), startTime, endTime, imgs, shopImgs, ShopManageActivity.this, MyState.requestTwo);
            }
        });
        httpCent.shopInfo(this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                StoreBean bean = JSONObject.parseObject((String) data, StoreBean.class);
                tvShopName.setText(bean.getName());
                tvShopAddress.setText(bean.getAddress());
                tvShopPhone.setText(bean.getPhone());
                startTime = bean.getStart_time();
                endTime = bean.getEnd_time();
                tvShopTime.setMsg(bean.getStart_time() + "-" + bean.getEnd_time());
                if (!TextUtils.isEmpty(bean.getImgs())) {
                    imgs = bean.getImgs();
                } else {
                    imgs = "";
                }
                //photosList = MyUtils.getListStringSplitValue(imgs);
                choosePhotoListViewAdapter = new ChoosePhotoListViewAdapter(photosList, this);
                gvShopImg.setAdapter(choosePhotoListViewAdapter);
                choosePhotoListViewAdapter.setAdapterClickListener(this);
                gvShopImg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        if (photosList.get(i).equals("add")) {
                            MyUtils.startPhotoPicker(ShopManageActivity.this, 1, MyState.PHOTO_PICKER_CODE_ONE);
                        }
                    }
                });
                setImagePath(imgs);
                if (!TextUtils.isEmpty(bean.getShopimg())) {
                    shopImgs = bean.getShopimg();
                } else {
                    shopImgs = "";
                }
                //photosList = MyUtils.getListStringSplitValue(imgs);
                choosePhotoListViewAdapters = new ChoosePhotoListViewAdapters(photosLists, this);
                gvShopHome.setAdapter(choosePhotoListViewAdapters);
                choosePhotoListViewAdapters.setAdapterClickListener(this);
                gvShopHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        if (photosLists.get(i).equals("add")) {
                            MyUtils.startPhotoPicker(ShopManageActivity.this, 1, MyState.PHOTO_PICKER_CODE_TWO);
                        }
                    }
                });
                setImagePaths(shopImgs);
                break;
            case MyState.requestTwo:
                mToast("信息修改成功");
                break;
            case MyState.requestThree:
                if (imgs.equals("")) {
                    imgs = (String) data;
                } else {
                    imgs = imgs + "," + data;
                }
                setImagePath(imgs);
                break;
            case MyState.requestFour:
                if (shopImgs.equals("")) {
                    shopImgs = (String) data;
                } else {
                    shopImgs = shopImgs + "," + data;
                }
                setImagePaths(shopImgs);
                break;
        }
    }


    public void setImagePath(String path) {
        photosList.clear();
        if (path != null && !"".equals(path)) {
            if (!path.contains(",")) {
                photosList.add(path);
            } else {
                for (int i = 0; i < path.split(",").length; i++) {
                    photosList.add(path.split(",")[i]);
                }
            }
        }
        if (photosList.size() < 5) {
            photosList.add("add");
        }
        choosePhotoListViewAdapter.notifyDataSetChanged();
    }

    public void setImagePaths(String path) {
        photosLists.clear();
        if (path != null && !"".equals(path)) {
            if (!path.contains(",")) {
                photosLists.add(path);
            } else {
                for (int i = 0; i < path.split(",").length; i++) {
                    photosLists.add(path.split(",")[i]);
                }
            }
        }
        if (photosLists.size() < 5) {
            photosLists.add("add");
        }
        choosePhotoListViewAdapters.notifyDataSetChanged();
    }

    @Override
    public void deletePhoto(int pos) {
        photosList.remove(pos);
        imgs = "";
        for (int i = 0; i < photosList.size(); i++) {
            if (!photosList.get(i).equals("add")) {
                imgs = imgs + "," + photosList.get(i);
            }
        }
        if (imgs != null && !"".equals(imgs)) {
            imgs = imgs.substring(1, imgs.length());
        }
        setImagePath(imgs);
    }

    public void onTimePicker(final View view) {
        TimePicker picker = new TimePicker(this, TimePicker.HOUR_24);
        picker.setRangeStart(9, 0);
        picker.setRangeEnd(18, 0);
        picker.setSelectedItem(showDate.get(Calendar.HOUR_OF_DAY), showDate.get(Calendar.MINUTE));
        picker.setTopLineVisible(false);
        picker.setLineVisible(false);
        picker.setWheelModeEnable(false);
        picker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
            @Override
            public void onTimePicked(String hour, String minute) {
                startTime = hour + ":" + minute;
                onTimePickers(view);
            }
        });
        picker.show();
    }

    private Calendar showDate;

    public void onTimePickers(View view) {

        TimePicker picker = new TimePicker(this, TimePicker.HOUR_24);
        picker.setRangeStart(9, 0);
        picker.setRangeEnd(18, 0);
        picker.setSelectedItem(showDate.get(Calendar.HOUR_OF_DAY), showDate.get(Calendar.MINUTE));
        picker.setTopLineVisible(false);
        picker.setLineVisible(false);
        picker.setWheelModeEnable(false);
        picker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
            @Override
            public void onTimePicked(String hour, String minute) {
                endTime = hour + ":" + minute;
                tvShopTime.setMsg(startTime + "-" + endTime);
            }
        });
        picker.show();
    }


    @OnClick({R.id.tv_shop_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_shop_time:
                onTimePicker(view);
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        MyUtils.hideSoftInput(this);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case MyState.PHOTO_PICKER_CODE_ONE:
                    List<String> list = new ArrayList<>();
                    list.addAll(data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS));
                    if (list.size() > 0) {
                        ImageTools.loadImage(this, list.get(0), this, MyState.requestThree);
                    }
                    break;
                case MyState.PHOTO_PICKER_CODE_TWO:
                    List<String> lists = new ArrayList<>();
                    lists.addAll(data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS));
                    if (lists.size() > 0) {
                        ImageTools.loadImage(this, lists.get(0), this, MyState.requestFour);
                    }
                    break;
            }
        }
    }

    @Override
    public void deletePhotos(int pos) {
        photosLists.remove(pos);
        shopImgs = "";
        for (int i = 0; i < photosLists.size(); i++) {
            if (!photosLists.get(i).equals("add")) {
                shopImgs = shopImgs + "," + photosLists.get(i);
            }
        }
        if (shopImgs != null && !"".equals(shopImgs)) {
            shopImgs = shopImgs.substring(1, shopImgs.length());
        }
        setImagePaths(shopImgs);
    }
}
